/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import bean.StokBarang;
import bean.PemesananBarang;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tea milandi
 */
@WebServlet(urlPatterns = {"/PesanBarang"})
public class PesanBarang extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        PemesananBarang pemesanan = new PemesananBarang();
        String jumlah_barang = request.getParameter("jumlahbarang");
        String id_barang = request.getParameter("idbarang");
        if ("".equals(jumlah_barang) || "".equals(id_barang)) {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("PesanBarang.jsp");
            requestDispatcher.forward(request, response);
        } else {
            StokBarang barangPusat = new StokBarang();
            barangPusat.setIdBarang(id_barang);
            barangPusat.setJumlahBarang(Double.parseDouble(jumlah_barang));
            if (barangPusat.bandingJumlahBarang() == true) {
                double jum = Double.parseDouble(jumlah_barang);
                pemesanan.setJumlahBarang(jum);
                pemesanan.setIdBarang(id_barang);
                Cookie[] ck = request.getCookies();
                pemesanan.setUsername(ck[1].getValue());
                pemesanan.setStatus("belum");
                pemesanan.insertPemesanan();
                out.println("<html>\n"
                        + "    <head>\n"
                        + "        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
                        + "        <title>Data Tersimpan</title>\n"
                        + "    </head>\n"
                        + "    <body bgcolor=\"FFC373\"> \n"
                        + "    <center>\n"
                        + "        <br><br><br><br><br><br><br><br><br>\n"
                        + "        <table border=\"0\" width=\"700\">\n"
                        + "            <tr>\n"
                        + "                <td colspan=\"2\" align=\"center\"><img src=\"gambar/logo asmart.png\" width=\"300\" height=\"84\"/></td>\n"
                        + "            </tr>\n"
                        + "            <tr>\n"
                        + "                <td colspan=\"2\" align=\"center\"\"><font face=\"Disko\" size=\"8px\">DATA TELAH TERSIMPAN</font></td>\n"
                        + "            </tr>\n"
                        + "        </table>\n"
                        + "        <table border=\"0\" align=\"center\" width=\"300px\">\n"
                        + "            <tr>\n"
                        + "                <td align=\"center\" width=\"43%\"><a href=\"PesanBarang.jsp\"><img src=\"gambar/back_button.png\" height=\"40\" width=\"120\"></a></td>\n"
                        + "            </tr>\n"
                        + "        </table>\n"
                        + "    </center>\n"
                        + "</body>\n"
                        + "</html>\n"
                        + "\n"
                        + "");
            } else {
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("PesanBarang.jsp");
                requestDispatcher.forward(request, response);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(PesanBarang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(PesanBarang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
